import { Global, Logger, LoggerService, Module } from "@nestjs/common";
import { ConfigModule, ConfigService } from "@nestjs/config";
import * as dotenv from "dotenv";
import * as Joi from "joi";
import { TypeOrmModule } from "@nestjs/typeorm";
import { connectionParams } from "../ormconfig";
import { ConfigEnum } from "./enum/config.enum";
import { UserModule } from "@/modules/user/user.module";
import { RoleModule } from "@/modules/role/role.module";
import { APP_FILTER, APP_GUARD } from "@nestjs/core";
import { AuthModule } from "@/modules/auth/auth.module";
import { JwtAuthGuard } from "@/guard/jwt-auth.guard";
import { UserRoleModule } from './modules/user-role/user-role.module';

const envFilePath = `.env.${process.env.NODE_ENV || `development`}`;

const schema = Joi.object({
	NODE_ENV: Joi.string()
		.valid("development", "production", "test")
		.default("development"),
	DB_PORT: Joi.number().default(3306),
	DB_HOST: Joi.alternatives().try(Joi.string().ip(), Joi.string().domain()),
	DB_TYPE: Joi.string().valid("mysql", "postgres"),
	DB_DATABASE: Joi.string().required(),
	DB_USERNAME: Joi.string().required(),
	DB_PASSWORD: Joi.string().required(),
	DB_SYNC: Joi.boolean().default(false),
	LOG_ON: Joi.boolean(),
	LOG_LEVEL: Joi.string()
});


@Global()
@Module({
	imports: [
		ConfigModule.forRoot({
			isGlobal: true,
			envFilePath,
			// https://github.com/nestjs/config/issues/209#issuecomment-625765057
			// load方法需要自己加入验证
			// 解决方法：https://dev.to/rrgt19/ways-to-validate-environment-configuration-in-a-forfeature-config-in-nestjs-2ehp
			load: [
				() => {
					const values = dotenv.config({ path: ".env" });
					const { error } = schema.validate(values?.parsed, {
						// 允许未知的环境变量
						allowUnknown: true,
						// 如果有错误，不要立即停止，而是收集所有错误
						abortEarly: false
					});
					if (error) {
						throw new Error(
							`Validation failed - Is there an environment variable missing?
        ${error.message}`
						);
					}
					return values;
				}
			],
			validationSchema: schema
		}),
		// Redis集成
		// RedisModule.forRootAsync({
		//   useFactory: (configService: ConfigService, logger: LoggerService) => {
		//     const host = configService.get(ConfigEnum.REDIS_HOST);
		//     const port = configService.get(ConfigEnum.REDIS_PORT);
		//     const password = configService.get(ConfigEnum.REDIS_PASSWORD);
		//     const url = password
		//       ? `redis://${password}@${host}:${port}`
		//       : `redis://${host}:${port}`;
		//     return {
		//       config: {
		//         url,
		//         reconnectOnError: (err) => {
		//           logger.error(`Redis Connection error: ${err}`);
		//           return true;
		//         },
		//       },
		//     };
		//   },
		//   inject: [ConfigService, Logger],
		// }),
		TypeOrmModule.forRoot(connectionParams),
		UserModule,
		RoleModule,
		AuthModule,
		UserRoleModule
	],
	controllers: [],
	providers: [
		Logger,
		//注册全局过滤器
		// {
		//     provide: APP_FILTER,
		//     useClass: HttpExceptionFilter,
		// },
		// {
		//   provide: APP_GUARD,
		//   useClass: AdminGuard,
		// },
		{
			provide: APP_GUARD,
			useClass: JwtAuthGuard
		}
	],
	exports: [Logger]
})
export class AppModule {
}
